---
--- Created by admin.
--- DateTime: 2017/10/4 16:45
---
--require("utils.loggerUtils")
--require("utils.StringUtils")
--require("util")

FuZhai = {}
FuZhai.UI = function()
x, y = findMultiColorInRegionFuzzy(0x671f0e,"-7|-15|0xfef9eb,-16|-10|0xfefbef,-12|1|0x5a1809,22|1|0x611d0c,-134|47|0x7f796b,-143|38|0x252227,-128|89|0xc8992b,-319|47|0xe8b847,20|92|0x8b6645,22|74|0xc4a786,23|89|0x936f4f", 85, 0, 0, 719, 1279, 0, 0)

  if x > -1 then
    logger.info("府宅界面")
    return true
  end
end

FuZhai.UIClose = function()
x, y = findMultiColorInRegionFuzzy(0x671f0e,"-7|-15|0xfef9eb,-16|-10|0xfefbef,-12|1|0x5a1809,22|1|0x611d0c,-134|47|0x7f796b,-143|38|0x252227,-128|89|0xc8992b,-319|47|0xe8b847,20|92|0x8b6645,22|74|0xc4a786,23|89|0x936f4f", 85, 0, 0, 719, 1279, 0, 0)
  if x > -1 then
    shouzhi.click(x, y)
    logger.info("府宅界面关闭")
    return true
  end
end

FuZhai.huaAn = function()
  x, y = findMultiColorInRegionFuzzy(0xd8b6a3, "-39|0|0x2e292f,-16|67|0x252420,26|209|0xd5b19f,-10|275|0x11161d,73|237|0x8f857d,94|95|0x19120e,106|71|0xfed573,-15|224|0x14161d,-57|162|0x01050a,-92|99|0x271c20,28|286|0x11151c,-66|301|0x293540,-5|289|0x12141b", 95, 20, 576, 282, 1012, 0, 0)
  if x > -1 then
    shouzhi.click(x, y)
    logger.info("华安")
    return true
  end
end


FuZhai.hongYan = function()
  
  x, y = findMultiColorInRegionFuzzy(0xe9d7c9, "2|50|0xe2c8b6,-7|186|0xdfbf9e,-21|131|0xe4dfde,51|134|0xb8aead,-77|44|0x1b130f,-81|6|0x130b08,24|270|0xe0deed,-79|227|0x92887f,-32|-47|0x4a4a45,17|-8|0x150f0b", 95, 7, 606, 710, 1063, 0, 0)
  if x > -1 then
    shouzhi.click(x, y)
    logger.info("红颜")
    return true
  end
end


FuZhai.ziSi = function()
  x, y = findMultiColorInRegionFuzzy(0xe9ddcf, "0|-34|0xd6c8b9,15|-26|0x4b496b,-57|-20|0x19120e,-70|-56|0x130b08,-4|-59|0xd6b299,44|-45|0xe2ded9,-28|-37|0xd0c0af,-25|0|0x757396,9|82|0xf2ecfa,-6|39|0xcfcad0,-41|15|0x95877d,-19|-75|0x110708", 95, 13, 797, 710, 1085, 0, 0)
  if x > -1 then
    shouzhi.click(x, y)
    logger.info("子嗣")
    return true
  end
end


FuZhai.hongYanUi = function()
  
  x, y = findMultiColorInRegionFuzzy(0x9d2410, "-59|-2|0x98220d,-24|15|0x594942,-93|-6|0x5e5045,-353|87|0x826048,-516|85|0xf1ead5,-475|83|0x9b7a63,-488|95|0x6d574a,-352|87|0x826048,-403|114|0x3b2b21,-599|119|0x3b2b21,-124|62|0x6d574a,-124|40|0x685245", 95, 7, 7, 717, 177, 0, 0)
  if x > -1 then
    
    logger.info("红颜界面")
    return true
  end
end


FuZhai.hongYanUiClose = function()
  
  x, y = findMultiColorInRegionFuzzy(0x9d2410, "-59|-2|0x98220d,-24|15|0x594942,-93|-6|0x5e5045,-353|87|0x826048,-516|85|0xf1ead5,-475|83|0x9b7a63,-488|95|0x6d574a,-352|87|0x826048,-403|114|0x3b2b21,-599|119|0x3b2b21,-124|62|0x6d574a,-124|40|0x685245", 95, 7, 7, 717, 177, 0, 0)
  if x > -1 then
    shouzhi.click(x, y)
    logger.info("红颜界面关闭")
    return true
  end
end

FuZhai.HongYanSuiJiChuanHuan = function()
  
  x, y = findMultiColorInRegionFuzzy(0xc9ad8b, "-52|0|0xc9ad8b,67|-1|0xcbaf8d,71|36|0xa17956,-9|37|0xa17956,-49|36|0xa17956,41|55|0x6d574a,-87|0|0x6e584b,-13|-26|0x685245,4|-33|0x685245,16|-19|0x6d574a,-3|-15|0x6a5447,34|53|0x6b5448", 95, 440, 1121, 719, 1271, 0, 0)
  if x > -1 then
    shouzhi.click(x, y)
    logger.info("随机传唤")
    return true
  end
end


FuZhai.hongYanBuKanZiSi = function()
  x, y = findMultiColorInRegionFuzzy(0xa17956, "-49|-18|0xb4926f,32|-18|0xb4926f,-258|-27|0xc4a785,-322|-24|0xbf9f7d,-292|1|0xa17956,-183|4|0x120d0a,-399|-15|0x120d0a,110|-15|0x120d0a,-31|-54|0x120d0a,-217|-90|0xfce538,-255|-80|0xfdbb1a,-19|-92|0xfced37,18|-84|0xfdce2b", 95, 76, 850, 623, 1024, 0, 0)
  if x > -1 then
    shouzhi.click(x, y)
    logger.info("不看子嗣")
    return true
  end
end

FuZhai.hongyanHuiFuTiLi = function()
  x, y = findMultiColorInRegionFuzzy(0xb21f00, "-62|0|0xb21f00,58|0|0xb21f00,56|37|0x780100,9|38|0x780000,-59|34|0x7c0300,-95|2|0x6c5649,13|-19|0x6d574a,41|56|0x6b5448,-18|58|0x6b5548,3|-32|0x685245,-5|-14|0x6a5447,-20|-24|0x685245,-92|6|0x6d574a,-92|45|0x6d574a", 95, 484, 1164, 712, 1269, 0, 0)
  if x > -1 then
    logger.info("没体力了")
    return true
  end
end

FuZhai.hongYanBuShiYongYao = function()
  x, y = findMultiColorInRegionFuzzy(0xba9876, "-22|-15|0xc7ab89,101|-14|0xc6a987,-3|20|0xa17956,79|18|0xa17956,-294|-12|0xc3a583,-195|-15|0xc7ab89,-305|15|0xa27a57,-195|17|0xa17956,-104|-156|0xe336b0,-110|-148|0xd233a3,-110|-180|0xae4d8a,-139|-119|0x000000", 95, 64, 472, 610, 781, 0, 0)
  if x > -1 then
    shouzhi.click(x, y)
    logger.info("不是用药")
    return true
  end
end

FuZhai.jieSuoZiSi = function()
  x, y = findMultiColorInRegionFuzzy(0xc3bdc1, "-8|51|0xa8a09e,46|-64|0xd0bfae,-20|-72|0xd0c0af,2|-98|0xd4af95,18|-101|0xe5c0a6,-27|-102|0xa79d94,-55|-58|0xc79603,-22|-147|0xdbbb30,-44|-148|0xe4b523,-55|-136|0xe4b21e,-55|-111|0xe8b722,6|-39|0xeadecf", 95, 33, 727, 713, 1111, 0, 0)
  if x > -1 then
    logger.info("解锁子嗣")
    return true
  end
end

FuZhai.shiYe = function()
  x, y = findMultiColorInRegionFuzzy(0xc1becd, "-3|-16|0x232227,-2|-73|0x1d1816,-18|-13|0xdcd9eb,-17|28|0x131214,-3|32|0xb5aaa6,22|40|0x221e1d,-11|95|0xa1a0aa,13|-11|0xa9a5b8,-62|19|0x19120e,-76|-17|0x130b08,-16|129|0x98979f,-36|77|0x2d292a,-18|-12|0xdcd9eb", 95, 2, 487, 718, 908, 0, 0)
  if x > -1 then
    shouzhi.click(x, y)
    logger.info("师爷")
    return true
  end
end

FuZhai.ZiChanUI = function()
  x, y = findMultiColorInRegionFuzzy(0xa0230d, "-50|-9|0xa02410,-86|-11|0x5e5045,-55|9|0x574942,22|10|0x5c4d46,-205|55|0x43342c,-430|53|0x43342c,-218|-12|0x6b594b,-328|4|0x2d1e1b,-135|8|0x2d1e1b,-403|154|0xcaa853,-421|59|0xe4bf7c,-422|68|0xd9a85c,-186|55|0x000406,-191|55|0x031a17,-178|73|0xa8a194,-90|153|0xb58d53,-298|128|0x4a372f", 95, 207, 5, 718, 244, 0, 0)
  if x > -1 then
    logger.info("资产界面")
    return true
  end
end

FuZhai.ZiChanUIClose = function()
  x, y = findMultiColorInRegionFuzzy(0xa0230d, "-50|-9|0xa02410,-86|-11|0x5e5045,-55|9|0x574942,22|10|0x5c4d46,-205|55|0x43342c,-430|53|0x43342c,-218|-12|0x6b594b,-328|4|0x2d1e1b,-135|8|0x2d1e1b,-403|154|0xcaa853,-421|59|0xe4bf7c,-422|68|0xd9a85c,-186|55|0x000406,-191|55|0x031a17,-178|73|0xa8a194,-90|153|0xb58d53,-298|128|0x4a372f", 95, 207, 5, 718, 244, 0, 0)
  if x == -1 then
    x, y = findMultiColorInRegionFuzzy(0x981602, "30|4|0x9f2009,-42|1|0x5e5044,0|31|0x6d564e,-228|16|0x2d1e1b,-88|13|0x2d1e1b,-145|61|0x000406,-148|56|0xd15730,-149|82|0x979079,-161|67|0x43342c,-388|59|0x43342c,-400|52|0x705a4d,-347|88|0x43342c,-78|42|0x685245", 95, 1, 28, 717, 214, 0, 0)
    
  end
  if x > -1 then
    shouzhi.click(x, y)
    logger.info("资产界面关闭")
    return true
  end
end


FuZhai.ziCHanJingYing = function()

x, y = findMultiColorInRegionFuzzy(0xe0a92d,"-23|-1|0xe1ab2f,-12|-17|0xf1cd4d,103|-14|0xefc748,98|17|0xd7991f,1|16|0xd7991f,-21|65|0x352522,17|65|0x352522,77|70|0x362522", 85, 444, 370, 660, 1197, 0, 0)


  if x > -1 then
    shouzhi.click(x, y)
    --logger.info("资产经营")
    return true
  end
end

FuZhai.ziChanWanCheng = function()
  
  x, y = findMultiColorInRegionFuzzy(0xab1b00, "52|-10|0xb82200,1|26|0x7b0200,-10|-340|0xab1b00,61|-350|0xb92300,6|-312|0x790100,1|-677|0xa61800,67|-684|0xb21f00,16|-654|0x7c0200,-5|-893|0xb58d53,82|-893|0xb58d53,-8|-558|0xb58d53,56|-559|0xb58d53,-32|-214|0xb58d53,46|-214|0xb58d53", 95, 339, 160, 695, 1254, 0, 0)
  if x > -1 then
    logger.info("完成经营")
    return true
  end
end


FuZhai.zhenWuUi = function()
  x, y = findMultiColorInRegionFuzzy(0x921802, "-54|1|0x9b1d07,-90|4|0x594b40,-53|16|0x574741,-2|15|0x504139,-180|43|0x262228,-181|107|0x252227,-364|46|0x252228,-347|50|0xdaa53e,-341|51|0xfcdf8c,-356|90|0xd6a958,-356|90|0xd6a958,-164|90|0xeedb73,-164|91|0xedd568,-175|98|0xca8f1d,-180|106|0x252227,-315|137|0x221f21,-72|141|0x221f21,-16|108|0x8b6544,-17|86|0xc7aa89", 95, 282, 0, 719, 307, 0, 0)
  if x > -1 then
    logger.info("政务界面")
    return true
  end
end


FuZhai.zhenWuUiClose = function()
  
  x, y = findMultiColorInRegionFuzzy(0x921802, "-54|1|0x9b1d07,-90|4|0x594b40,-53|16|0x574741,-2|15|0x504139,-180|43|0x262228,-181|107|0x252227,-364|46|0x252228,-347|50|0xdaa53e,-341|51|0xfcdf8c,-356|90|0xd6a958,-356|90|0xd6a958,-164|90|0xeedb73,-164|91|0xedd568,-175|98|0xca8f1d,-180|106|0x252227,-315|137|0x221f21,-72|141|0x221f21,-16|108|0x8b6544,-17|86|0xc7aa89", 90, 282, 0, 719, 307, 0, 0)
  if x > -1 then
    shouzhi.click(x, y)
    logger.info("政务界面关闭")
    return true
  end
end



FuZhai.zhenWuZiYuan = function()
  x, y = findMultiColorInRegionFuzzy(0x735e50, "369|-65|0x221f21,2|0|0x745e50,-7|120|0x6e594c,27|53|0x221f21,128|55|0x221f21,37|158|0x221f21,197|157|0x221f21,-45|-9|0x221f21", 95, 5, 958, 718, 1199, 0, 0)
  if x > -1 then
    shouzhi.click(x, y)
    logger.info("政务获取资源")
    return true
  end
end

FuZhai.zhenWuZhengJi = function()
  x, y = findMultiColorInRegionFuzzy(0x756052, "374|-177|0x221f21,7|-112|0x745e50,-2|8|0x6e594c,32|-59|0x221f21,133|-57|0x221f21,42|46|0x221f21,202|45|0x221f21,-40|-121|0x221f21", 95, 5, 958, 718, 1199, 0, 0)
  if x > -1 then
    shouzhi.click(x, y)
    logger.info("政务获取政绩")
    return true
  end
end

FuZhai.zhengWuMeiYouZhenWu = function()
  x, y = findMultiColorInRegionFuzzy(0xcdb18f, "-43|17|0xb59370,-37|75|0x5e4e45,-40|-31|0x5e4e45,-413|75|0x5e4e45,-540|-5|0xf89b21,-519|-4|0xf6a23d,-545|21|0xfda121,-524|25|0xf49920,-569|-14|0xaa3712,-553|-34|0xb93c15,-568|55|0x150500,-490|51|0x140500", 95, 12, 943, 710, 1234, 0, 0)
  if x > -1 then
    logger.info("没有政务需要处理")
    return true
  end
end


FuZhai.ziSiUi = function()
  
  x, y = findMultiColorInRegionFuzzy(0x9e2812, "-62|-2|0x9e2513,-88|-3|0x5d4f44,-31|14|0x564740,-20|30|0x605555,-167|12|0x2d1e1b,-294|10|0x2d1e1b,-325|35|0x605555,-149|144|0xd2c46b,-117|134|0xccc165,-326|68|0x5f5454,-201|71|0x5f5454,-167|68|0x5f5555", 95, 209, 30, 717, 207, 0, 0)
  if x > -1 then
    logger.info("子嗣界面")
    return true
  end
end


FuZhai.ziSiQuMing = function()
  x, y = findMultiColorInRegionFuzzy(0xb42000, "107|0|0xb42000,-1|33|0x790100,100|33|0x790100,-397|85|0x1fa7cc,-401|91|0x0f559b,-393|91|0x0f559b", 95, 79, 539, 696, 764, 0, 0)
  if x > -1 then
    shouzhi.click(x, y)
    logger.info("子嗣取名")
    return true
  end
end

FuZhai.ziSiQuMingQueRen = function()
  x, y = findMultiColorInRegionFuzzy(0xb99775,"114|-2|0xbc9c7a,235|4|0xb28e6c,342|-1|0xba9a78,31|-58|0x47342b,284|-55|0x47342b,200|-65|0x49362d,370|-147|0xfba134,-50|-109|0x301f18,-41|-93|0x9d7860,62|-221|0x291405,231|-238|0x291405,248|-226|0x291405,383|-147|0xcb843c,382|-115|0xfcd5af,382|-115|0xfcd5af,-79|-96|0x48352c", 85, 34, 448, 690, 875, 0, 0)
  
  if x > -1 then
    shouzhi.click(x, y)
    logger.info("子嗣取名确认")
    return true
  end
end
FuZhai.ziSiPeiYang = function()
  x, y = findMultiColorInRegionFuzzy(0xf4d150, "101|2|0xf2ce4e,-5|29|0xd79a20,109|32|0xd7991f,47|36|0xd7991f,-400|84|0x21b4d5,-400|92|0x0d4994", 95, 79, 539, 696, 764, 0, 0)
  if x > -1 then
    shouzhi.click(x, y)
    logger.info("子嗣培养")
    return true
  end
  
end

FuZhai.ziSiMeiTili = function()
  
  x, y = findMultiColorInRegionFuzzy(0x930e00, "-17|-18|0xb52100,98|-18|0xb52100,-8|15|0x7a0200,96|17|0x780100,-400|71|0x292d38,-406|71|0x292d39", 95, 79, 539, 696, 764, 0, 0)
  if x > -1 then
    logger.info("没体力了")
    return true
  end
end



FuZhai.ziSiBuShiYongYao = function()
  
  x, y = findMultiColorInRegionFuzzy(0xb69471, "-23|-22|0xceb290,102|-22|0xceb290,-8|19|0xa17956,90|16|0xa17956,-203|-19|0xcaae8c,-300|-17|0xc7ab89,-306|13|0xa27a57,-202|19|0xa17956,-116|-173|0x0036a9,-85|-158|0x1a57bb,-101|-153|0x226fbb", 95, 79, 484, 651, 791, 0, 0)
  if x > -1 then
    shouzhi.click(x, y)
    logger.info("取消使用药")
    return true
  end
end


FuZhai.ziSiUiClose = function()
  
  x, y = findMultiColorInRegionFuzzy(0x9e2812, "-62|-2|0x9e2513,-88|-3|0x5d4f44,-31|14|0x564740,-20|30|0x605555,-167|12|0x2d1e1b,-294|10|0x2d1e1b,-325|35|0x605555,-149|144|0xd2c46b,-117|134|0xccc165,-326|68|0x5f5454,-201|71|0x5f5454,-167|68|0x5f5555", 95, 209, 30, 717, 207, 0, 0)
  if x > -1 then
    shouzhi.click(x, y)
    logger.info("子嗣界面关闭")
    return true
  end
end


FuZhai.hongYanRun = function()
  FuZhai.hongYan()
  local flag = FuZhai.hongYanUi()
  if not flag then
    return false
  end
  
  while true do
    
    shouzhi.clickDevToRun( 513, 123)
    FuZhai.HongYanSuiJiChuanHuan()
    FuZhai.hongYanBuKanZiSi()
    FuZhai.hongYanBuShiYongYao()
    if FuZhai.hongyanHuiFuTiLi() then
      break
    end
    
    -- 点击
    shouzhi.click(716, 297)
    mSleep(500)
  end
  
  mSleep(1000)
  FuZhai.hongYanUiClose()
  mSleep(500)
  FuZhai.jieSuoZiSi()
end


FuZhai.ZiChanRun = function()
  FuZhai.huaAn()
  local flag = FuZhai.ZiChanUI()
  if not flag then
    return false
  end
  
  while true do
    FuZhai.ziCHanJingYing()
    -- 完成经营
    if FuZhai.ziChanWanCheng() == true then
      break
    end
    mSleep(1500)
  end
  logger.info("准备关闭资产界面")
  FuZhai.ZiChanUIClose()
  return true
end

FuZhai.ziSiRun = function()
  
  local isRun = true;
  
  local functions = { FuZhai.ziSiQuMing, FuZhai.ziSiQuMingQueRen,FuZhai.ziSiPeiYang, FuZhai.ziSiBuShiYongYao }
  while isRun do
    isRun = false
    FuZhai.ziSi()
    local flag = FuZhai.ziSiUi()
    if not flag then
      return false
    end
    
    
    while true do
      for i, func in ipairs(functions) do
        local tempFlag = func()
        if tempFlag then
          isRun = tempFlag
        end
      end
      if FuZhai.ziSiMeiTili() then
        break
      end
      --
      shouzhi.click(716, 297)
    end
    mSleep(1000)
    FuZhai.ziSiUiClose()
    mSleep(1000)
  end
  
  
end


FuZhai.zhengWuRun = function()
  
  FuZhai.shiYe()
  local flag = FuZhai.zhenWuUi()
  if not flag then
    return false
  end
  
  while true do
    if Setting.fuzhai_zhenJi_type == Setting.fuzhai_zhenJi_ziYuan then
      FuZhai.zhenWuZiYuan()
    else
      FuZhai.zhenWuZhengJi()
    end
    
    -- 完成政务
    if FuZhai.zhengWuMeiYouZhenWu() == true then
      break
    end
    mSleep(500)
  end
  
  FuZhai.zhenWuUiClose()
  return true
end

FuZhai.onlyZiChanRun = function()
  logger.info("只运行资产")
  while true do
    FuZhai.ziCHanJingYing()
    -- 完成经营
    mSleep(1500)
  end
end


FuZhai.run = function()
  logger.info("府宅功能运行")
  
  shouzhi.move(111, 425, 604, 427)
  shouzhi.move(111, 425, 604, 427)
  
  for i = 1, 2 do
    logger.info("检测可操作事物")
    
    FuZhaiFJ.run()
    mSleep(500)
    if Setting.fuzhai_ziyuan then
      FuZhai.ZiChanRun()
      mSleep(100)
    end
    
    if Setting.fuzhai_zhengji then
      FuZhai.zhengWuRun()
      mSleep(100)
    end
    
    if Setting.fuzhai_hongyan then
      FuZhai.hongYanRun()
      mSleep(100)
    end
    
    if Setting.fuzhai_zishi then
      FuZhai.ziSiRun()
      mSleep(100)
    end
    mSleep(1000)
    
    
    shouzhi.move(604, 427, 111, 425)
    shouzhi.move(604, 427, 111, 425 )
  end
  
end


